// pages/order-details/order-details.js
const apiurl = require('../../config').apiurl;
import { Pulibc } from '../../models/public.js'
const pulibc = new Pulibc()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail:{},
    allBuynum:0
  },
  // 申请发货
  deliverGoods(e){
    let goods_id = e.currentTarget.dataset.goods_id
    let order_id = e.currentTarget.dataset.order_id
    let rec_id = e.currentTarget.dataset.rec_id
    let url = apiurl + '/order/shipment';
    let key = wx.getStorageSync('key');
    wx.request({
      url: url,
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: { key, goods_id, order_id, rec_id},
      success:data=>{
        console.log(data)
        if(data.data.code == 200){
          pulibc.showToast(data.data.info)
        }else{
          pulibc.showToast(data.data.datas.error,'none')
        }
      }
    })
  },
  // 申请退货
  showSales(e) {
    // 
   
    let goods_id = e.currentTarget.dataset.goods_id
    let order_id = e.currentTarget.dataset.order_id

    pulibc.navigateUrl('../sales-return/sales-return?goods_id=' + goods_id + '&order_id=' + order_id);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let order_id=options.order_id;
    this.getOrderDetail(order_id);
  },
  // 复制订单编号
  copyBtn(){
    let data = this.data.orderDetail.order_sn;
    wx.setClipboardData({
      data: data.toString(),
      success:res=>{
        // console.log(res)
        // pulibc.showToast('复制成功','none')
      }
    })
  },
  // 获取订单详情
  getOrderDetail(order_id){
    let url = apiurl +'/order/order_info';
    let params={};
    params.key=wx.getStorageSync('key');
    params.order_id=order_id;
    wx.request({
      url: url,
      data:params,
      header:{
        'Content-type':'application/x-www-form-urlencoded'
      },
      method:'POST',
      success:res=>{
        console.log(res)
        let orderDetail=res.data.datas.info;
        let allBuynum=0;
        orderDetail.goods_list.map(item=>{
          allBuynum += parseInt(item.goods_num);
        })
        this.setData({
          orderDetail,
          allBuynum
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})